/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package usuario.control;

import bibliinfo.FrameMain;
import javax.swing.JOptionPane;
import javax.swing.plaf.basic.BasicInternalFrameUI;
import usuario.model.Contato;
import usuario.model.Data;
import usuario.model.Endereco;
import usuario.model.Usuario;
import usuario.model.UsuarioDao;
import usuario.model.ValidaUsuario;

/**
 *
 * @author Ar0n
 */
public class UserFrameDadosUpdate extends javax.swing.JInternalFrame {

    /**
     * Creates new form UserFrameDadosSalve
     */
    public UserFrameDadosUpdate() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jBAdd = new javax.swing.JButton();
        jBCancel = new javax.swing.JButton();
        jBSave = new javax.swing.JButton();

        jBAdd.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jBAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/img_user/1365839912_netvibes.png"))); // NOI18N
        jBAdd.setText("Dados Adicionais");
        jBAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAddActionPerformed(evt);
            }
        });

        jBCancel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jBCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/img_user/1365683699_window-close.png"))); // NOI18N
        jBCancel.setText("Cancelar");
        jBCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCancelActionPerformed(evt);
            }
        });

        jBSave.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jBSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/img_user/1365288060_3floppy_umount.png"))); // NOI18N
        jBSave.setText("Salvar");
        jBSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBSaveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jBAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 185, Short.MAX_VALUE)
                .addComponent(jBCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jBSave, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(40, 40, 40))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBSave, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(70, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCancelActionPerformed
        
       FrameMain.userFrameDados.setVisible(false);
       FrameMain.userFrameDados.setEstado(false);
       
       FrameMain.userFrameMain.setVisible(true);
       FrameMain.userFrameMain.setEstado(true); 
        
        //retirando bordas do internalJframe           
       ((BasicInternalFrameUI)FrameMain.userFrameMain.getUI()).setNorthPane(null);
       FrameMain.userFrameMain.setBorder(null);        
       //pegando dimenções do JFrame no qual ele esta encapsulado
       FrameMain.userFrameMain.setBounds(0, 0, FrameMain.jDesktop.getWidth(), FrameMain.jDesktop.getHeight());        
       FrameMain. userFrameMain.setResizable(false);
       FrameMain.userFrameMain.setBorder(null);         
    }//GEN-LAST:event_jBCancelActionPerformed

    private void jBAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAddActionPerformed
       FrameMain.userFrameDados.setVisible(false);
       FrameMain.userFrameDados.setEstado(false);

       FrameMain.userFrameAdd.setVisible(true);
       FrameMain.userFrameAdd.setEstado(true);
       FrameMain.mapa.put("frame13", FrameMain.userFrameAdd);
       FrameMain.formata(13);               
       
       FrameMain.userFrameAdd.setComponente(2);           
    }//GEN-LAST:event_jBAddActionPerformed

    private void jBSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBSaveActionPerformed
        ValidaUsuario validarUsuario = new ValidaUsuario();
        boolean resp = validarUsuario.validaUserUpdate(FrameMain.userFrameDados.getRgAntigo(), FrameMain.userFrameDados.jTRG, FrameMain.userFrameDados.jTNome,
                FrameMain.userFrameDados.jTCidade, FrameMain.userFrameDados.jTCep, FrameMain.userFrameDados.jTBairro,
                FrameMain.userFrameDados.jTRua, FrameMain.userFrameDados.jTNumCasa, FrameMain.userFrameDados.jTDia,
                FrameMain.userFrameDados.jTMes, FrameMain.userFrameDados.jTAno);
        
        if(resp == true){
            
            resp = validarUsuario.validarAdd(FrameMain.userFrameAdd.jTDataCadDia, FrameMain.userFrameAdd.jTDataCadMes,
                    FrameMain.userFrameAdd.jTDataCadAno, FrameMain.userFrameAdd.jRBSim.isSelected(), FrameMain.userFrameAdd.jTDataInicioDia, FrameMain.userFrameAdd.jTDataInicioMes,
                    FrameMain.userFrameAdd.jTDataInicioAno, FrameMain.userFrameAdd.jTDataFimDia, FrameMain.userFrameAdd.jTDataFimMes,
                    FrameMain.userFrameAdd.jTDataFimAno);
            
            if(resp == true){
                atualizarUser();
            }            
        }
    }//GEN-LAST:event_jBSaveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBAdd;
    private javax.swing.JButton jBCancel;
    private javax.swing.JButton jBSave;
    // End of variables declaration//GEN-END:variables
    
    //métodos
    public void atualizarUser(){
        
        Usuario usuario = new Usuario();
        usuario.setRg(FrameMain.userFrameDados.getRgAntigo());
        usuario.setRgNovo(Long.parseLong(FrameMain.userFrameDados.jTRG.getText()));
        usuario.setNome(FrameMain.userFrameDados.jTNome.getText());
        usuario.setSexo(FrameMain.userFrameDados.jCBSexo.getSelectedItem().toString());
        usuario.setAtivo(true);
        if(FrameMain.userFrameAdd.jRBNao.isSelected() == true){
            usuario.setSuspenso(false);
        }else{
            usuario.setSuspenso(true);
        }        
        usuario.setObservacoes(FrameMain.userFrameDados.jTAObs.getText());
        
        Endereco endereco = new Endereco();        
        endereco.setRg(usuario.getRgNovo());
        endereco.setEstado(FrameMain.userFrameDados.jCBEstado.getSelectedItem().toString());
        endereco.setCidade(FrameMain.userFrameDados.jTCidade.getText());        
        endereco.setCep(FrameMain.userFrameDados.jTCep.getText());
        endereco.setBairro(FrameMain.userFrameDados.jTBairro.getText());
        endereco.setRua(FrameMain.userFrameDados.jTRua.getText());
        endereco.setNumCasa(FrameMain.userFrameDados.jTNumCasa.getText());
        
        Contato contato = new Contato();
        contato.setRg(usuario.getRgNovo());
        contato.setTel_1(FrameMain.userFrameDados.jTTel_1.getText());
        contato.setTel_2(FrameMain.userFrameDados.jTTel_2.getText());
        contato.setTel_3(FrameMain.userFrameDados.jTTel_3.getText());
        contato.setTel_4(FrameMain.userFrameDados.jTTel_4.getText());
        contato.setEmail_1(FrameMain.userFrameDados.jTEmail_1.getText());
        contato.setEmail_2(FrameMain.userFrameDados.jTEmail_2.getText());        
        
        Data data = new Data();
        data.setRg(usuario.getRgNovo());
        int dia = Integer.parseInt(FrameMain.userFrameDados.jTDia.getText());
        int mes = Integer.parseInt(FrameMain.userFrameDados.jTMes.getText());
        int ano = Integer.parseInt(FrameMain.userFrameDados.jTAno.getText());
        data.calendar.set(ano, mes -1, dia);
        data.instanciarDataNasc();
        data.InstanciarDataNascSQL();
        
        dia = Integer.parseInt(FrameMain.userFrameAdd.jTDataCadDia.getText());
        mes = Integer.parseInt(FrameMain.userFrameAdd.jTDataCadMes.getText());
        ano = Integer.parseInt(FrameMain.userFrameAdd.jTDataCadAno.getText());
        data.calendar.set(ano, mes -1, dia);
        data.instanciarDataCad();
        data.instanciarDataCadSQL();
        
        if(usuario.getSuspenso() == true){
            dia = Integer.parseInt(FrameMain.userFrameAdd.jTDataInicioDia.getText());
            mes = Integer.parseInt(FrameMain.userFrameAdd.jTDataInicioMes.getText());
            ano = Integer.parseInt(FrameMain.userFrameAdd.jTDataInicioAno.getText());
            data.calendar.set(ano, mes -1, dia);
            data.instanciarDataInicioMulta();
            data.instanciarDataInicioMultaSQL();

            dia = Integer.parseInt(FrameMain.userFrameAdd.jTDataFimDia.getText());
            mes = Integer.parseInt(FrameMain.userFrameAdd.jTDataFimMes.getText());
            ano = Integer.parseInt(FrameMain.userFrameAdd.jTDataFimAno.getText());
            data.calendar.set(ano, mes -1, dia);
            data.instanciarDataFimMulta();
            data.instanciarDataFimMultaSQL();
        }else{
            dia = 00;
            mes = 00;
            ano = 0002;
            data.calendar.set(ano, mes, dia);
            data.instanciarDataInicioMulta();
            data.instanciarDataInicioMultaSQL();
            
            data.instanciarDataFimMulta();
            data.instanciarDataFimMultaSQL();
        }        
        
        UsuarioDao dao = new UsuarioDao();
        dao.update(usuario, endereco, contato, data);
        
        //fechando frame e limpando campos
        if(FrameMain.userFrameDados.getEstado() == true){
            JOptionPane.showMessageDialog(FrameMain.userFrameDados, "Os dados foram atualizados com sucesso", "", 1);
            FrameMain.userFrameDados.habilitaCampos();
            
        }else if(FrameMain.userFrameAdd.getEstado() == true){
            JOptionPane.showMessageDialog(FrameMain.userFrameAdd, "Os dados foram atualizados com sucesso", "", 1);
            FrameMain.userFrameDados.habilitaCampos();
            
        }        
            UserFrameDadosSave sv = new UserFrameDadosSave();
            sv.limparCampos();

            FrameMain.userFrameDados.setVisible(false);
            FrameMain.userFrameDados.setEstado(false);

            FrameMain.userFrameAdd.setVisible(false);
            FrameMain.userFrameAdd.setEstado(false);
            
            FrameMain.userFrameMain.setVisible(true);
            FrameMain.userFrameMain.setEstado(true);
            FrameMain.formata(11);
        
    }
}
